package com.rentplant.controller;


import com.alibaba.fastjson.JSONObject;
import com.rentplant.Pojo.rental;
import com.rentplant.Pojo.rentalanduser;
import com.rentplant.service.impl.rentalServiceImp;
import org.junit.platform.commons.function.Try;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController//让它知道这是controller
@RequestMapping("/rental")
public class rentalController {

    @Autowired
    private rentalServiceImp rentalserviceimp;


    //新增租房信息
    @RequestMapping("/addRental")
    public boolean addRental(@RequestBody JSONObject req){
        try {
            return rentalserviceimp.addRental(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //查询最新租房信息
    @RequestMapping("/rentalByNew")
    public List<rentalanduser> rentalByNew(){
        try {
            return rentalserviceimp.rentalByNew();
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }
    //查询价格最高的租房信息
    @RequestMapping("/rentalByHigh")
    public List<rentalanduser> rentalByHigh(){
        try {
            return rentalserviceimp.rentalByHigh();
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }
    //查询最热租房信息
    @RequestMapping("/rentalByHot")
    public List<rentalanduser> rentalByHot(){
        try {
            return rentalserviceimp.rentalByHot();
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }
    //查询价格最低的租房信息
    @RequestMapping("/rentalByLow")
    public List<rentalanduser> rentalByLow(){
        try{
            return rentalserviceimp.rentalByLow();
        }catch (Exception e){
            throw new RuntimeException(e);
        }

    }

    //按照id查询租房信息
    @RequestMapping("/rentalById")
    public List<rentalanduser> rentalById(@RequestBody JSONObject req){
        try {
            return rentalserviceimp.rentalById(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }


    //更新浏览量
    @RequestMapping("/updateSee")
    public  boolean updateSee(@RequestBody JSONObject req){
        try {
            return rentalserviceimp.updateSee(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //展示用户的租房信息
    @RequestMapping("/showUserRental")
    public  List<rental> showUserRental(@RequestBody JSONObject req){
        try {
            return rentalserviceimp.showUserRental(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //    展示用户已删除租房信息
    @RequestMapping("/delHaveRental")
    public  List<rental> delHaveRental(@RequestBody JSONObject req){
        try {
            return rentalserviceimp.delHaveRental(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //    重新上架用户租房信息
    @RequestMapping("/rentalState")
    public boolean rentalState(@RequestBody  JSONObject req){
        try {
//            System.out.println(req);
            return rentalserviceimp.rentalState(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //删除用户发布的租房信息
    @RequestMapping("/changeRentalState")
    public boolean changeRentalState(@RequestBody  JSONObject req){
        try {
//            System.out.println(req);
            return rentalserviceimp.changeRentalState(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //    租房管理端
    @RequestMapping("/allRental")
    public List<rental> allRental(@RequestBody JSONObject req){
        try {
            return rentalserviceimp.allRental(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }
    @RequestMapping("/allRentals")
    public List<rental> allRentals(){
        try {
            return rentalserviceimp.allRentals();
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //    修改租房信息
    @RequestMapping("/updateRental")
    public boolean updateRental(@RequestBody JSONObject req){
        try {
            return rentalserviceimp.updateRental(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //    条件查询
    @RequestMapping("/partRental")
    public List<rental> partRental(@RequestBody JSONObject req){
        try {
            return rentalserviceimp.partRental(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //    条件查询
    @RequestMapping("/partRentals")
    public List<rental> partRentals(@RequestBody JSONObject req){
        try {
            return rentalserviceimp.partRentals(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

}
